import axios from 'axios'

const CancelToken = axios.CancelToken
export const source = CancelToken.source()

/**
 * @typedef {import('axios')} Axios
 * @typedef {{
    request: import('axios').AxiosInterceptorManager<import('axios').AxiosRequestConfig>;
    response: import('axios').AxiosInterceptorManager<import('axios').AxiosResponse>;
  }} Interceptors
 */

/**
 * @param {import('axios').AxiosRequestConfig} config
 * @param {Interceptors} interceptors
 */
export function createService(config, interceptors) {
  const AxiosInstance = axios.create(config)

  AxiosInstance.interceptors.request.use(interceptors.request)
  AxiosInstance.interceptors.response.use(...interceptors.response)

  return AxiosInstance
}
